package com.beerq.view;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.beerq.R;
import com.beerq.util.BasicHttp;
import com.beerq.util.BeerQPref;
import com.beerq.util.Constants;
import com.beerq.util.StringUtil;
import com.beerq.view.friends.FriendsManager;
import com.beerq.view.me.LoginActivity;
import com.beerq.view.me.MyCollectActivity;
import com.beerq.view.me.OtherActivity;
import com.beerq.view.me.PersonalSetting;
import com.beerq.view_manager.RegisterAndLoginManager;
import com.bumptech.glide.Glide;

import static com.beerq.util.Constants.userInfo;

/**
 * A simple {@link Fragment} subclass.
 */
public class ForthFragment extends Fragment {

    View mView_no_login;
    View mView_login_in;

    TextView mTxtShowCarTip;
    TextView mTxtWaittingPayTip;
    TextView mTxtWaittingGoTip;
    TextView mTxtWaittingRecevierTip;
    TextView mTxtWaittingEvlauteTip;

    TextView mTxtBeerMan;


    TextView mTxt_desc;

    String strUserInfo;


    private TextView mTxt_score;

    public ForthFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_forth_2, container, false);
    }




    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if(!hidden){
            getData();
        }

    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mTxtShowCarTip=(TextView)getActivity().findViewById(R.id.txt_shopcar_tip);
        mTxtWaittingPayTip=(TextView)getActivity().findViewById(R.id.txt_waittingpay_tip);
        mTxtWaittingGoTip=(TextView)getActivity().findViewById(R.id.txt_waittinggo_tip);
        mTxtWaittingRecevierTip=(TextView)getActivity().findViewById(R.id.txt_waittingreceiver_tip);
        mTxtWaittingEvlauteTip=(TextView)getActivity().findViewById(R.id.txt_waittingevlaute_tip);

        mTxtBeerMan=(TextView) getActivity().findViewById(R.id.txt_beer_man);

        strUserInfo=BeerQPref.getInstance(getActivity()).getUserInfo();

        mView_no_login=getActivity().findViewById(R.id.layout_login_out);
        mView_login_in=getActivity().findViewById(R.id.layout_login_in);

        mTxt_score=(TextView) getActivity().findViewById(R.id.txt_score);


        mTxt_desc=(TextView)getActivity().findViewById(R.id.txt_desc) ;


        mTxt_score.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(),OtherActivity.class);
                intent.putExtra("typee",1);
                intent.putExtra("val",mTxt_score.getTag().toString());
                startActivity(intent);
            }
        });

        bindLoginInEvent();

        getActivity().findViewById(R.id.btn_login_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),LoginActivity.class);
                getActivity().startActivity(intent);
            }
        });

        mTxtBeerMan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),OtherActivity.class);
                intent.putExtra("typee",2);
                intent.putExtra("val",mTxtBeerMan.getText().toString());
                getActivity().startActivity(intent);
            }
        });



    }

    @Override
    public void onResume() {
        super.onResume();


        strUserInfo=BeerQPref.getInstance(getActivity()).getUserInfo();
        if(StringUtil.isEmpty(strUserInfo)){
            mView_no_login.setVisibility(View.VISIBLE);
            mView_login_in.setVisibility(View.GONE);
        }else{
            TextView txtName=(TextView)getActivity().findViewById(R.id.txt_name);
            mView_login_in.setVisibility(View.VISIBLE);
            mView_no_login.setVisibility(View.GONE);

            if(StringUtil.isEmpty(strUserInfo)){
                return;
            }


            txtName.setText(userInfo.getUserName());

            ImageView imageView = (ImageView) (getActivity()).findViewById(R.id.img_head_pic);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(getActivity(),ModifyHeadImageActivity.class);
                    startActivity(intent);
                }
            });

//            RequestOptions options = new RequestOptions();
//            options.centerCrop()
//                    .placeholder(R.drawable.user_logo)
//                    .error(R.drawable.user_logo)
//                    .fallback(R.drawable.user_logo);
            Glide.with(getActivity()).load(userInfo.getHeadPic()).placeholder(R.drawable.user_logo).error(R.drawable.user_logo).fallback(R.drawable.user_logo).into(imageView);
            getData();
        }
    }
    private void getData(){

        if(userInfo==null){
            return;
        }

        getMyFavouriteAndLikes();

        mTxtShowCarTip.setVisibility(View.GONE);
        mTxtWaittingPayTip.setVisibility(View.GONE);
        mTxtWaittingGoTip.setVisibility(View.GONE);
        mTxtWaittingRecevierTip.setVisibility(View.GONE);
        mTxtWaittingEvlauteTip.setVisibility(View.GONE);

        RegisterAndLoginManager.getPQHXData(getActivity(), userInfo.getAppUserId(), new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(String resonseData) {

                if(JSONObject.parseObject(resonseData).getJSONArray("Result")!=null && JSONObject.parseObject(resonseData).getJSONArray("Result").size()>0){
                    ((ImageView)getActivity().findViewById(R.id.img_pqhz)).setImageDrawable(getActivity().getResources().getDrawable(R.drawable.pqhz1));
                }
//                JSONObject jsonObject= JSONObject.parseObject(resonseData).getJSONArray("Result").getJSONObject(0);


            }

            @Override
            public void onFailure(String resonseData) {

            }
        });


        RegisterAndLoginManager.getOrderData(getActivity(), userInfo.getAppUserId(), new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(String resonseData) {

                JSONObject jsonObject= JSONObject.parseObject(resonseData).getJSONArray("Result").getJSONObject(0);

                if(jsonObject.getIntValue("MyCartCount")>0){
                    mTxtShowCarTip.setText(jsonObject.getString("MyCartCount"));
                    mTxtShowCarTip.setVisibility(View.VISIBLE);
                }

                if(jsonObject.getIntValue("WaitPayCount")>0){
                    mTxtWaittingPayTip.setText(jsonObject.getString("WaitPayCount"));
                    mTxtWaittingPayTip.setVisibility(View.VISIBLE);
                }

                if(jsonObject.getIntValue("WaitSendCount")>0){
                    mTxtWaittingGoTip.setText(jsonObject.getString("WaitSendCount"));
                    mTxtWaittingGoTip.setVisibility(View.VISIBLE);
                }

                if(jsonObject.getIntValue("WaitFinishCount")>0){
                    mTxtWaittingRecevierTip.setText(jsonObject.getString("WaitFinishCount"));
                    mTxtWaittingRecevierTip.setVisibility(View.VISIBLE);
                }

                if(jsonObject.getIntValue("FinishCount")>0){
                    mTxtWaittingEvlauteTip.setText(jsonObject.getString("FinishCount"));
                    mTxtWaittingEvlauteTip.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(String resonseData) {

            }
        });


    }

    private void getMyFavouriteAndLikes(){
        FriendsManager.getMyFavouriteAndLikes(getActivity(), userInfo.getAppUserId(), new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(String resonseData) {
                JSONObject temp= JSONObject.parseObject(resonseData).getJSONArray("Result").getJSONObject(0);
                mTxt_score.setTag(temp.getString("CapCash"));
                mTxtBeerMan.setText(temp.getString("GradeName"));
                mTxt_score.setText(temp.getString("CapCash")+"瓶盖  /  "+temp.getString("MyFocusCount")+"关注  /  "+temp.getString("FocusMeCount")+"粉丝");

                mTxt_desc.setText("啤气值："+temp.getString("Bouns")+"  距"+temp.getString("LeftUpGradeName")+"还差："+temp.getString("LeftUpGradeBouns"));

            }

            @Override
            public void onFailure(String resonseData) {

            }
        });
    }

    private void bindLoginInEvent(){
        getActivity().findViewById(R.id.txt_order).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",Constants.domain+"#/myorder");
                intent.putExtra("barType","1");
                startActivity(intent);
            }
        });

        getActivity().findViewById(R.id.img_services).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ServicesActivity.class);
                getActivity().startActivity(intent);
            }
        });

        //脾气盒子
        getActivity().findViewById(R.id.rel_sign).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("barType","1");
                intent.putExtra("url", Constants.domain+"#/box");


                ProductJsManager.setUserInfo(Constants.firstWebView, userInfo.getAppUserId());


                getActivity().startActivity(intent);
            }
        });


        //购物车
        getActivity().findViewById(R.id.lin_shop_car).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("barType","1");
                intent.putExtra("url", Constants.domain+"#/shopcar?fromPage=goods");
                getActivity().startActivity(intent);
            }
        });

        //红包
        getActivity().findViewById(R.id.lin_red_evelope).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",Constants.domain+"#/hongbao");
                intent.putExtra("barType","1");
                startActivity(intent);
            }
        });

        getActivity().findViewById(R.id.rel_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), PersonalSetting.class);
                startActivity(intent);
            }
        });



        getActivity().findViewById(R.id.lin_collect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), MyCollectActivity.class);
                startActivity(intent);
            }
        });

        getActivity().findViewById(R.id.lin_watting_pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",Constants.domain+"#/myorder?orderstatus=1");
                intent.putExtra("barType","1");
                startActivity(intent);
            }
        });
        getActivity().findViewById(R.id.lin_watting_go).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",Constants.domain+"#/myorder?orderstatus=2");
                intent.putExtra("barType","1");
                startActivity(intent);
            }
        });
        getActivity().findViewById(R.id.lin_watting_receiver).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",Constants.domain+"#/myorder?orderstatus=3");
                intent.putExtra("barType","1");
                startActivity(intent);
            }
        });
        getActivity().findViewById(R.id.lin_watting_evaluate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",Constants.domain+"#/myorder?orderstatus=4");
                intent.putExtra("barType","1");
                startActivity(intent);
            }
        });



    }


}
